package com.societyGames.codeAlong.state.codeExercise.view.status
{
import com.societyGames.flashForms.DynamicTextField;
import com.societyGames.flashForms.ItemsContainer;

import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;

public class Status extends Sprite
{
  public var highlightName:String;
  private var _textField:TextField;
  private var _mainContainer:Sprite;
  private var _centerItem:DisplayObject;
  private var _desiredWidth:int;
  private var _desiredHeight:int;

  public function Status(backgroundClass:Class)
  {
    this._textField = new DynamicTextField(); //SharedTextFields.createHtmlTextField();
    this._textField.selectable = false;
    this._textField.autoSize = TextFieldAutoSize.LEFT;

    this._centerItem = this._textField;

    var itemsContainer:ItemsContainer = new ItemsContainer();
    itemsContainer.addItem(new backgroundClass());
    itemsContainer.addItem(this._centerItem);

    this._mainContainer = itemsContainer;
    this.addChild(this._mainContainer);
  }

  override public function set width(value:Number):void
  {
    this._desiredWidth = value;
    recalculate();
  }

  override public function set height(value:Number):void
  {
    this._desiredHeight = value;
    recalculate();
  }

  private function recalculate():void
  {
    this._mainContainer.width = this._desiredWidth;
    this._mainContainer.height = this._desiredHeight;
    if (this._centerItem.width < this._desiredWidth)
    {
      this._centerItem.x = (this._desiredWidth - this._centerItem.width) / 2;
    }
    else
    {
      this._centerItem.x = 0;
    }
  }

  public function set htmlText(value:String):void
  {
    this._textField.htmlText = value;
  }
}
}